package com.memory.ui;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.memory.util.MemoryUtility;

public class MemoryMatrixActivity extends Activity
		implements
			View.OnClickListener {
	private TableLayout layout;
	private Button b;
	private ArrayList<Button> mArrayList;
	private int mst[][];
	private Vector<Integer> a;
	private int size;
	private boolean wrongSelection=false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		layout = new TableLayout(this);
		layout.setBackgroundColor(Color.WHITE);
		mArrayList = new ArrayList<Button>();
		layout.setLayoutParams(new TableLayout.LayoutParams(20, 20));
		layout.setPadding(15, 15, 15, 15);
		init();
		setContentView(layout);
	}

	public void init() {
		mst = new int[MemoryUtility.TOTAL_TILES][MemoryUtility.TOTAL_TILES];
		int incr = 0;
		for (int f = 0; f < MemoryUtility.TOTAL_TILES; f++) {
			TableRow tr = new TableRow(this);
			tr.setPadding(5, 15, 5, 15);
			for (int c = 0; c < MemoryUtility.TOTAL_TILES; c++) {
				b = new Button(this);
				b.setTag(incr);
				mst[f][c] = incr;
				b.setHeight(90);
				b.setWidth(90);
				b.setTextColor(Color.rgb(100, 200, 200));
				b.setOnClickListener(this);
				b.setPadding(5, 5, 5, 5);
				mArrayList.add(b);
				tr.addView(b, 90, 90);
				incr++;
			}
			layout.addView(tr);
		}
		mHandler.postDelayed(mRunnable, 5000);

		oldStage();
	}

	Handler mHandler = new Handler();
	Runnable mRunnable = new Runnable() {

		@Override
		public void run() {
			final Random myRandom = new Random();
			a = new Vector<Integer>();
			for (int i = 0; i < MemoryUtility.TOTAL_TILES; i++) {
				while(true)
			    {
					
					 Integer next = myRandom.nextInt(MemoryUtility.RANGE) + 1;
				        if (!a.contains(next))
				        {
				            a.add(next);
				            break;
				        }
				    
			    }
//				a.add(myRandom.nextInt(MemoryUtility.RANGE));
			}
			
			Log.i("DVO","the memory utility range is"+MemoryUtility.RANGE);
			size = a.size();
			for (Button k : mArrayList) {

				if (a.contains(k.getTag())) {
					k.setBackgroundColor(Color.RED);

				}
			}
		}

	};

	Runnable mRunnable2 = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (Button b : mArrayList) {
				b.setBackgroundResource(android.R.drawable.btn_default);
			}
		}

	};

	public void oldStage() {
		mHandler.postDelayed(mRunnable2, 10000);
		
	}
	public void onClick(View view) {
		
		Toast.makeText(this,"the size is"+size,Toast.LENGTH_SHORT);
		Log.i("DVO","the size is "+size);
		for (Button b : mArrayList) {
			if ((view == b) && (a.contains(b.getTag()))) {
				b.setBackgroundColor(Color.RED);
				size--;
				Log.i("DVO","the size is "+size);
				
				if (size == 0) {
					Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(this, NextTile.class);
					 MemoryUtility.TOTAL_TILES=MemoryUtility.TOTAL_TILES+1;
					 MemoryUtility.RANGE=(MemoryUtility.TOTAL_TILES * 2) - 1;
					startActivity(intent);
				}
				wrongSelection=true;
			}
		}
		if (wrongSelection==false) {
			Toast.makeText(this, "Wrong Selection", Toast.LENGTH_LONG).show();
			Intent intent = new Intent(this, NextTile.class);
			 MemoryUtility.TOTAL_TILES=MemoryUtility.TOTAL_TILES-1;
			 MemoryUtility.RANGE=(MemoryUtility.TOTAL_TILES / 2) - 1;
			startActivity(intent);
		}

	}

}
